﻿
using LYR_RPA.Properties;
using LYR_RPA.service;
using Microsoft.Win32;
using System.Windows;
using System.Windows.Controls;

namespace LYR_RPA.pages.set
{
    /// <summary>
    /// BasicSetPage.xaml 的交互逻辑
    /// </summary>
    public partial class BasicSetPage : Page
    {
        private readonly WebSocketClient WebSocketClient;
        public BasicSetPage()
        {
            InitializeComponent();
            WebSocketClient = WebSocketClient.Instance;
            UIRobotPath.Text = Settings.Default.RobotPath;
            if (!string.IsNullOrEmpty(Settings.Default.ServerAddress))
            {
                ServerAddress.Text= Settings.Default.ServerAddress;
                
            }
            if(!string.IsNullOrEmpty(Settings.Default.Port))
            {
                Port.Text= Settings.Default.Port;
            }
        }

        private void SelectFileButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择文件",
                Filter = "应用程序 (*UiRobot.exe*)|*.exe*" // 可以根据需要设置文件类型过滤
            };

            bool? result = openFileDialog.ShowDialog();

            if (result == true)
            {
                string selectedFilePath = openFileDialog.FileName;
                Settings.Default.RobotPath = selectedFilePath;
                Settings.Default.Save();
                UIRobotPath.Text = Settings.Default.RobotPath;
            }
        }

        private void Restart_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default.RobotPath = App.RobotPath;
            Settings.Default.Save();
            UIRobotPath.Text = Settings.Default.RobotPath;
        }

        private async void Determine_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(ServerAddress.Text) || string.IsNullOrEmpty(Port.Text))
            {
                Settings.Default.ServerAddress = ServerAddress.Text;
                Settings.Default.Port = Port.Text;
                Settings.Default.Save();
                WebSocketClient.Close();
            }
            else
            {
                Settings.Default.ServerAddress = ServerAddress.Text;
                Settings.Default.Port = Port.Text;
                Settings.Default.Save();
                await WebSocketClient.Connect();
            }


        }
    }
}
